import { getCollectCaseList } from '../../Service/ColloctionService';
const appInstance = getApp();

Page({
  data: {
    medifoodCollections: [], // 存放药膳收藏列表数据
    loading: true, // 加载状态
  },

  onLoad() {
    // this.loadMedifoodCollections();

  },

  onShow() {
    if (!appInstance.checkTokenValidity()) {
      // Token 已过期或不存在，已重新登录
      console.log('Token expired, re-login in progress');
    } else {
      console.log('Token is valid');
    }
    this.loadMedifoodCollections();
  },

  async loadMedifoodCollections() {
    try {
      const userId = wx.getStorageSync('userId'); // 从缓存中获取 userId
      if (!userId) {
        throw new Error('用户未登录');
      }
      const collections = await getCollectCaseList(userId); // 传递 userId 作为参数
      // console.log('收藏列表数据:', collections);

      this.setData({
        medifoodCollections: collections.data,
        loading: false, // 数据加载完成
      });
      console.log('收藏列表', this.data.medifoodCollections);
    } catch (error) {
      console.error('获取药膳收藏列表失败:', error);
      this.setData({ loading: false });
    }
  },
});
